package bluetoothcomm;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import monitor.StreamEvent;
import monitor.StreamEventListener;


public class TwoWayComm implements StreamEventListener,ObstructionEventListener
{
	
	public static SerialWriter mySerialWriter;
	public static SerialReader mySerialReader;
	int numLine;
	CommPort commPort;
	Enumeration			portList;
    CommPortIdentifier	portId;
    SerialPort			serialPort;
    static InputStream			in;
    static OutputStream 		out;
    boolean portOpened = false;
    String defaultPort = "/dev/tty.FireFly-57F9-SPP";
	
    public TwoWayComm()
    {
        super();
    }
      
    public void connect() 
    {        
        
		portList = CommPortIdentifier.getPortIdentifiers();

		while (portList.hasMoreElements()) {
		    portId = (CommPortIdentifier) portList.nextElement();
		    System.out.println(portId.getName());
		    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				if (portId.getName().equals(defaultPort)) {
				    try {
				    	serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000);
					    System.out.println("Opened port " + defaultPort);
					    try {
					    	out = serialPort.getOutputStream();
					    	in = serialPort.getInputStream();
			                
			                mySerialWriter = new SerialWriter(out);
			                mySerialReader = new SerialReader(in) ;
			               
			                serialPort.addEventListener(mySerialReader);
			                serialPort.notifyOnDataAvailable(true);             
			                mySerialReader.addStreamListener(this);
			                mySerialReader.addObstructionListener(this);
						} catch (TooManyListenersException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					    try {
							in = serialPort.getInputStream();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				    	portOpened = true;
				    } catch (PortInUseException e) {
				    	System.out.println("Port in use.");
				    	continue;
				    } 
	
				    try {
				    	serialPort.setSerialPortParams(115200, 
								       SerialPort.DATABITS_8, 
								       SerialPort.STOPBITS_1, 
								       SerialPort.PARITY_NONE);
				    } catch (UnsupportedCommOperationException e) {}				    
				} 
		    } 
		} 
	}

	public void close() {
		if (portOpened) {
			serialPort.removeEventListener();
			serialPort.close();
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			portOpened = false;
		}
	}

	@Override
	public void streamAvailable(StreamEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("Printed from TwoWayComm");
		//System.out.print(e.getStr());
	}
	
	@Override
	public void ObstructionAvailable(ObstructionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("Obstruction : "+e.getStr());
	}
	
	public void sendMessageToPebble(String message){
		if(message.equals("AckConstruction")){
			mySerialWriter.send(170);
			mySerialWriter.send(67);
		}else if(message.equals("StartNavigation")){
			mySerialWriter.send(170);
			mySerialWriter.send(65);
		}else if(message.equals("EndNavigaion")){
			mySerialWriter.send(170);
			mySerialWriter.send(66);
		}
	}
}